package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: SGS
 * @Date : 2020/11/17 16:45
 * @Version :
 */
@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Resource
    private SpuService spuService;
    @Resource
    private SpecParamService specParamService;
    @Override
    public List<SpecParamDTO> queryParamBySpuId(Long id, Boolean searching) {
        SpuDetail detail = getById(id);
        //获取规格参数键值对转换为map
        Map<Long, Object> values = JsonUtils.toMap(detail.getSpecification(), Long.class, Object.class);
        //根据id查询spu,获取商品分类信息
        Spu spu = spuService.getById(id);
        //根据分类id查询规格参数集合,如果searching不为空,还要加上searching条件
        List<SpecParamDTO> params = specParamService.queryParams(spu.getCid3(), null, searching);
        //找到param的value,并存储
        for (SpecParamDTO param : params) {
            param.setValue(values.get(param.getId()));
        }
        return params;
    }
}
